/*
 * @lc app=leetcode.cn id=977 lang=java
 *
 * [977] 有序数组的平方
 */

// @lc code=start
class Solution {
    public int[] sortedSquares(int[] nums) {
        int left = 0;//头指针
        int right = nums.length - 1;//尾指针
        int[] res = new int[nums.length];//存放平方结果的数组
        int write=res.length-1;
        while (left <= right) {//其实就是两边选平方数最大的那个依次放到res中
            if (nums[left] * nums[left] > nums[right] * nums[right]) {
                res[write]=nums[left]*nums[left];
                left++;
                write--;
            }else{
                res[write]=nums[right]*nums[right];
                right--;
                write--;
            }
        }
        return res;

    }
}
// @lc code=end
